package JsonProcess;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import Storage.Buddy;
import android.location.Location;
import android.location.LocationManager;

public class jAllBuddies { // Parse JsonString
	public static ArrayList<Buddy> process(String jsonText)
			throws JSONException {
		ArrayList<Buddy> buddylist = new ArrayList<Buddy>();
		JSONArray array = new JSONArray(jsonText);
		
		for(int i = 0; i< array.length();i++)
		{
			Buddy buddy = new Buddy();
			Location location = new Location(LocationManager.GPS_PROVIDER);
			JSONObject object;
			object = (JSONObject) new JSONTokener(array.getString(i)).nextValue();
			location.setLatitude(Double.parseDouble(object.getString("lat")));
			location.setLongitude(Double.parseDouble(object.getString("log")));
			buddy.setFname(object.getString("firstname"));
			buddy.setLname(object.getString("lastname"));
			buddy.setLastUpdate(object.getString("lastupdate"));
			buddy.setPhoneNumber(object.getString("phonenumber"));
			buddy.setAddress(object.getString("address"));
			buddy.setEmail(object.getString("email"));
			buddy.setLocation(location);
			buddy.setIsOnline(object.getString("IsOnline"));
			buddy.setLastSeen(object.getString("lastseen"));
			
			buddylist.add(buddy);
		}		
		return buddylist;
	}

}
